#include "stdafx.h"
#include "FileSystem.h"
#include "KernelExports.h"

CXuiTextElement outsideElement;
CXuiList outsideList;
void dirSceneCallback(CXuiElement e) {
	CXuiTextElement text = (CXuiTextElement)e;
	if(text.GetText() == 0)
		return;

	outsideElement.SetText(text.GetText());

	char* str = new char[1024];
	WideCharToMultiByte(CP_UTF8, NULL, text.GetText(), -1, str, 1024, NULL, NULL);

	string path(str);
	delete str;

	outsideList.DeleteItems(0, outsideList.GetItemCount());

	path += "*.iso";

	wchar_t* wtext = new wchar_t[1024];

	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	hFind = FindFirstFile(path.c_str(), &wfd);
	if(hFind != INVALID_HANDLE_VALUE)
		do{
			if(!(wfd.dwFileAttributes & 0x10)) { // file
				outsideList.InsertItems(outsideList.GetItemCount(), 1);
			
				MultiByteToWideChar(CP_UTF8, NULL, wfd.cFileName, -1, wtext, 1024);

				outsideList.SetText(outsideList.GetItemCount() - 1, wtext);
			}
		} while(FindNextFile(hFind, &wfd));
	FindClose(hFind);

	delete wtext;
}

// Initialization
HRESULT CMyMainScene::OnInit(XUIMessageInit* pInitData, BOOL &bHandled) {
	// Get controls
	GetChildById(L"XuiButtonChangeDir", &buttonChangeDir);
	GetChildById(L"XuiButtonOptions", &buttonOptions);
	GetChildById(L"XuiButtonDashboard", &buttonDashboard);
	GetChildById(L"XuiCommonListGames", &listGames);
	GetChildById(L"XuiTextDir", &xuiTextDir);
	GetChildById(L"XuiButtonDiscTest", &buttonDiscTest);

	outsideElement = xuiTextDir;
	outsideList = listGames;

	xuiTextDir.SetText(L"");

	FileSystem::getInstance().MountDrives();

	DirSceneRegisterCallback(&dirSceneCallback);

	return S_OK;
}

	// Button press
HRESULT CMyMainScene::OnNotifyPress(HXUIOBJ hObjPressed, BOOL &bHandled) {
	// Check which button was pressed
	if( hObjPressed == buttonChangeDir) {
		// TODO: Dir browser
		HXUIOBJ hScene;
		XuiSceneCreate(L"file://game:/media/", L"changedirectory.xur", NULL, &hScene);
		NavigateForward(hScene, TRUE);
	}
	else if(hObjPressed == buttonOptions) {
		// TODO: Options dialog
		//Settings::getInstance().SaveSettings();
		//Settings::getInstance().LoadSettings();
	}
	else if(hObjPressed == buttonDashboard) {
		XLaunchNewImage(NULL, NULL); // Return to dash
	}
	else if(hObjPressed == listGames) {
		int listIndex = listGames.GetCurSel();
		LPCWSTR isoPathW = listGames.GetText(listIndex);
		int length = wcslen(isoPathW);
		char* isoPath = new char[length + 1];
		ZeroMemory(isoPath, length + 1);
		WideCharToMultiByte(CP_UTF8, NULL, isoPathW, -1, isoPath, length + 1, NULL, NULL);

		//k, now we have the path, tell the emulator to load it as the game, hide xui, and launch
		
		delete isoPath;
	}
	else if(hObjPressed == buttonDiscTest) {
		// Raw drive access!
/*
		ULARGE_INTEGER a, b, c;
		GetDiskFreeSpaceEx("DVD:\\", &a, &b, &c);
		double hddspace = (double)b.QuadPart / (1024*1024); // get hdd space in MB

		ANSI_STRING driveName;
		RtlInitAnsiString(&driveName, "\\Device\\CdRom0");
		HANDLE fHandle = NULL; IO_STATUS_BLOCK ioStatus;
		OBJECT_ATTRIBUTES objectAttr = { NULL, &driveName, 0 };
		NTSTATUS status = NtCreateFile(&fHandle, 0x80100000, &objectAttr, &ioStatus, 0, 4, 1, 1, 0x2A);

		LONGLONG position = 0x0000000000010000;
		status = NtSetInformationFile(fHandle, &ioStatus, &position, sizeof(LONGLONG), 0x0E);

		BYTE buffer[0x800];
		status = NtReadFile(fHandle, NULL, NULL, NULL, &ioStatus, buffer, 0x800, 0);
		
		NtClose(fHandle);*/
	}
	else
		return S_OK;

	bHandled = TRUE;
	return S_OK;
}